package com.rc.evcharger.redis;

import com.rc.evcharger.common.redis.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RedMemberServiceImpl implements RedMemberService {

    @Autowired
    private RedisDao redisDao;

    @Override
    public void addMember(String rtua, String gunNum, Long memberId) {
        RedMember member = new RedMember(rtua, gunNum, memberId);
        //redisDao.putSession(member.getKey(), member.getValue());
        redisDao.putRedis(member.getKey(), String.valueOf(memberId));
        //redisDao.expire(member.getKey(), 24*60*60);  //设置1天过期

    }

    @Override
    public Long getMemberId(String rtua, String gunNum) {
        RedMember member = new RedMember(rtua, gunNum);
        //return (Long) redisDao.getSession(member.getKey());
        String valueStr = redisDao.getRedis(member.getKey());
        if (valueStr == null) {
            return null;
        }
        return Long.valueOf(valueStr);
    }

    @Override
    public void removeMember(String rtua, String gunNum, Long memberId) {
        RedMember member = new RedMember(rtua, gunNum);
        String valueStr = redisDao.getRedis(member.getKey());
        if (valueStr == null) {
            return;
        }
        if (memberId == Long.valueOf(valueStr)) {
            redisDao.deleteSession(member.getKey());
        }
    }

    @Override
    public boolean hasMember(String rtua, String gunNum) {
        //RedMember member = new RedMember(rtua, gunNum);
        //List list = (List) redisDao.keys(member.getKey() + "*");
        //if (list.size() > 0) {
        //    return true;
        //}
        return false;
    }

}
